package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.User;
import cn.itcast.service.CheckGroupService;
import cn.itcast.service.SpringSecurityUserService;
import cn.itcast.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
/*import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;*/
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/user")
@CrossOrigin
public class UserController {
    @Reference
    private UserService userService;

    /**
     * 保存用户信息
     *

     * @return
     */
    @PostMapping()
    public Result add(@RequestBody User user,Integer[] roleIds) {
        try {
            userService.add(user,roleIds);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
    }

    /**
     * 分页查询用户信息
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping(path = "/page")
    public Result findPage(QueryPageBean queryPageBean) {
        //分页查询用户信息
        try {
            PageResult pageResult = userService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

    /**
     * 根据id查询用户详情
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            User user = userService.findById(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }


    /**
     * 查询用户下的角色ID集合
     *
     * @param userId
     * @return
     */
    @GetMapping(path = "/{id}/roleIds")
    public Result selectedroleIdsId(@PathVariable("id") Integer userId) {
        try {
            Integer[] userIds = userService.selectedroleIdsId(userId);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, userIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }


    /**
     * 更新用户信息
     *
     * @param user

     * @return
     */
    @PutMapping(path = "/update")
    public Result update(@RequestBody User user, Integer[] roleIds) {
        try {
            userService.update(user, roleIds);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_USER_FAIL);
        }
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(path = "/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        try {
            userService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

    /**
     * 查询所有用户信息
     *
     * @return
     */
    //@GetMapping(path = "/role/all")
    public Result findAll() {
        try {
            List<User> all = userService.findAll();
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, all);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }
    /*@GetMapping("getUsername")
    public Result getUsername(Authentication authentication) {
        try {
            String name = authentication.getName();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    @GetMapping("getMenus")
    public Result getMenus(Authentication authentication) {
        try {
            String username = authentication.getName();
            List<Map<String, Object>> menuList = userService.findMenus(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.GET_MENU_FAIL);
    }*/

}
